﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace NetCoreClass
{
    public static class myString
    {
        public static string replaceEncode(this string str)
        {
            return str.Replace("+", "%20").Replace("%3a", ":");
        }
        public static string clearHtml(this string strhtml)
        {
            Regex ar = new Regex("<[^>]+>", RegexOptions.Compiled);
            string output = strhtml.Replace(" ", "");
            output = ar.Replace(output, "");
            return output;
        }
        public static string chineseSubString(this string s, int l)
        {
            string temp = s;
            if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
            {
                return temp;
            }
            for (int i = temp.Length; i >= 0; i--)
            {
                temp = temp.Substring(0, i);
                if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
                {
                    return temp + "";
                }
            }
            return "";
        }
        #region 过滤特殊字符
        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="inputStr">字符串</param>
        /// <returns>string</returns>
        public static string clearBadStr(this string inputStr)
        {
            inputStr = inputStr.Replace(",", "");
            inputStr = inputStr.Replace("<", "&lt;");
            inputStr = inputStr.Replace(">", "&gt;");
            inputStr = inputStr.Replace("%", "");
            //inputStr = inputStr.Replace(".", "");
            inputStr = inputStr.Replace(":", "");
            inputStr = inputStr.Replace("#", "");
            inputStr = inputStr.Replace("&", "");
            inputStr = inputStr.Replace("$", "");
            inputStr = inputStr.Replace("^", "");
            inputStr = inputStr.Replace("*", "");
            inputStr = inputStr.Replace("`", "");
            // inputStr = inputStr.Replace(" ", "");
            inputStr = inputStr.Replace("~", "");

            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@" or ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            inputStr = regex1.Replace(inputStr, "");
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" and ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            inputStr = regex2.Replace(inputStr, "");
            return inputStr;

        }
        #endregion
        #region timespan

        /// <summary>
        /// 取时间戳，高并发情况下会有重复。想要解决这问题请使用sleep线程睡眠1毫秒。
        /// </summary>
        /// <param name="AccurateToMilliseconds">精确到毫秒</param>
        /// <returns>返回一个长整数时间戳</returns>
        public static long GetTimeStamp(bool AccurateToMilliseconds = false)
        {
            if (AccurateToMilliseconds)
            {

                // 使用当前时间计时周期数（636662920472315179）减去1970年01月01日计时周期数（621355968000000000）除去（删掉）后面4位计数（后四位计时单位小于毫秒，快到不要不要）再取整（去小数点）。

                //备注：DateTime.Now.ToUniversalTime不能缩写成DateTime.Now.Ticks，会有好几个小时的误差。

                //621355968000000000计算方法 long ticks = (new DateTime(1970, 1, 1, 8, 0, 0)).ToUniversalTime().Ticks;

                return (System.DateTime.Now.ToLocalTime().Ticks - 621355968000000000) / 10000;

            }
            else
            {
                //上面是精确到毫秒，需要在最后除去（10000），这里只精确到秒，只要在10000后面加三个0即可（1秒等于1000毫米）。
                return (System.DateTime.Now.ToLocalTime().Ticks - 621355968000000000) / 10000000;
            }
        }
        public static double GetTimeStampInt()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return ts.TotalSeconds;
        }
        #endregion
        /// <summary>
        /// 将UBB编码转成标准的HTML代码
        /// </summary>
        /// <param name="argString"></param>
        /// <returns></returns>
        public static string HtmlDecode(string argString)
        {
            string tString = argString;
            if (tString != "")
            {
                Regex tRegex;
                bool tState = true;
                tString = tString.Replace("&", "&amp;");
                tString = tString.Replace(">", "&gt;");
                tString = tString.Replace("<", "&lt;");
                tString = tString.Replace("\"", "&quot;");
                tString = Regex.Replace(tString, @"\[br\]", "<br />", RegexOptions.IgnoreCase);
                string[,] tRegexAry = {
          {@"\[p\]([^\[]*?)\[\/p\]", "$1<br />"},
          {@"\[b\]([^\[]*?)\[\/b\]", "<b>$1</b>"},
          {@"\[i\]([^\[]*?)\[\/i\]", "<i>$1</i>"},
          {@"\[u\]([^\[]*?)\[\/u\]", "<u>$1</u>"},
          {@"\[ol\]([^\[]*?)\[\/ol\]", "<ol>$1</ol>"},
          {@"\[ul\]([^\[]*?)\[\/ul\]", "<ul>$1</ul>"},
          {@"\[li\]([^\[]*?)\[\/li\]", "<li>$1</li>"},
          {@"\[code\]([^\[]*?)\[\/code\]", "<div class=\"ubb_code\">$1</div>"},
          {@"\[quote\]([^\[]*?)\[\/quote\]", "<div class=\"ubb_quote\">$1</div>"},
          {@"\[color=([^\]]*)\]([^\[]*?)\[\/color\]", "<font style=\"color: $1\">$2</font>"},
          {@"\[hilitecolor=([^\]]*)\]([^\[]*?)\[\/hilitecolor\]", "<font style=\"background-color: $1\">$2</font>"},
          {@"\[align=([^\]]*)\]([^\[]*?)\[\/align\]", "<div style=\"text-align: $1\">$2</div>"},
          {@"\[url=([^\]]*)\]([^\[]*?)\[\/url\]", "<a href=\"$1\">$2</a>"},
          {@"\[img\]([^\[]*?)\[\/img\]", "<img src=\"$1\" />"}
        };
                while (tState)
                {
                    tState = false;
                    for (int ti = 0; ti < tRegexAry.GetLength(0); ti++)
                    {
                        tRegex = new Regex(tRegexAry[ti, 0], RegexOptions.IgnoreCase);
                        if (tRegex.Match(tString).Success)
                        {
                            tState = true;
                            tString = Regex.Replace(tString, tRegexAry[ti, 0], tRegexAry[ti, 1], RegexOptions.IgnoreCase);
                        }
                    }
                }
            }
            return tString;
        }

        ///<summary>
        ///判断一个字符串是否在另一个字符串中
        ///</summary>
        ///<param name="strArray">
        ///用逗号分开的字符串
        ///</param>
        ///<param name="str">
        ///单一字符串
        ///</param>
        ///<returns>
        ///true/false
        ///</returns>


        public static bool inArray(string strArray, string str)
        {
            string[] idArray = strArray.Split(',');
            for (int i = 0; i < idArray.Length; i++)
            {
                if (str.Equals(idArray[i]))
                {
                    return true;
                }
            }
            return false;
        }
    }

}

